import { Component } from '@angular/core';
import {  NavController, NavParams, ToastController } from 'ionic-angular';
import { LoginDataProvider } from '../../providers/login-data/login-data';
import { User } from '../../providers/login-data/user';
import { TabsPage } from '../tabs/tabs';
import {Storage} from '@ionic/storage';




@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class LoginPage {
 
   user: User;
  constructor(public navCtrl: NavController,
     public navParams: NavParams,
     public toastCtrl:ToastController,
     public loginDataProvider:LoginDataProvider,
     public storage:Storage) {
  }

  ionViewDidLoad() {
  }
/**
 * 登录
 * @param username 
 * @param password 
 * @param remeber 
 */
  login(username:HTMLInputElement,password:HTMLInputElement,remember:HTMLInputElement){
    let _username = username.value.trim();
    let _password = password.value.trim();
    let _remember = remember.value;
    if(_username.length === 0){
      this.showToast('bottom','请输入账号');
      return;
    }
    if(_password.length ===0){
      this.showToast('bottom','请输入密码');
      return;
    }
    this.loginDataProvider.login(_username,_password).toPromise()
    .then(res=>{
     console.log('登录成功');
     console.log(res);
     this.user = res;
     return true ;
    },e=>{
      console.log("登录失败");
      console.log(e);
      return false;
    })
    .then(res=>{
      if(res){       //登录成功 存储相应信息
          let data ={
            username: this.user.username,
            objectId: this.user.objectId,
            sessionToken: this.user.sessionToken,
            remember: _remember
           }; 
           //保存”记住“的用户信息
           if(_remember){
             this.storage.remove('USER_INFO');
             this.storage.set('USER_INFO',JSON.stringify(data));
           } 
             //保存”登录“的用户信息
             this.storage.set('LOGIN_INFO',JSON.stringify(data));
             this.navCtrl.setRoot(TabsPage);   //清除之前栈，以当前栈为栈顶
        }else{
          this.showToast('bottom','用户名或密码错误');
        }
    });          


  }
/**
     * 注册
     * @param username 
     * @param password 
     */
    register(username: HTMLInputElement,password: HTMLInputElement){
      let _username = username.value.trim();
    let _password = password.value.trim();

    if(_username.length === 0){
      this.showToast('bottom','账号不能为空');
      return;
    }
    if(_password.length ===0){
      this.showToast('bottom','密码不能为空');
      return;
    }
    this.loginDataProvider.register(_username,_password).toPromise()
    .then(res=>{
     console.log('注册成功');
     console.log(res);
     this.user = res;
     return true ;
    },e=>{
      console.log("注册失败");
      console.log(e);
      return false;
    })
      .then(res =>{
        if(res){
          this.showToast('bottom','注册成功');
          this.navCtrl.setRoot(this.navCtrl.getActive().component);
        }else{
          this.showToast('bottom','账号以存在！');
        }
      });
    }


  /**
 * 显示提示信息
 * @param position 
 * @param message 
 */
    showToast(position: string ,message: string){
      let toast = this.toastCtrl.create({
        message: message,
        duration: 2000,
        position: position
      });
      toast.present();
    }
}
